/**
 * @file common.c
 * @brief Implements utility functions.
 * @author Vamshi Ponnapalli
 */
/*******************************************************************************
 * Includes
 ******************************************************************************/
#include "common.h"
#include "ioCC2530.h"

/*******************************************************************************
 * Function implementations
 ******************************************************************************/
void utils_reverseBuffer(uint8 *pBuffer, uint16 size)
{
   uint8 temp;
   uint8 *pBufferEnd = (pBuffer + size - 1);

   while(pBufferEnd > pBuffer)
   {
     temp = *pBuffer;
     *pBuffer++ = *pBufferEnd;
     *pBufferEnd-- = temp;	
  }
}
/******************************************************************************/
/**
 * Sleeps for the specified number of microseconds.
 */
#pragma optimize=none
void utils_sleepUs(uint16 duration)
{
  duration >>= 1;
  while(duration--)
  {
    NOP();
    NOP();
    NOP();
    NOP();
    NOP();
    NOP();
    NOP();
    NOP();
    NOP();
    NOP();
    NOP();
    NOP();
    NOP();
    NOP();
    NOP();
    NOP();
    NOP();
  }
}
/******************************************************************************/
void utils_initializeRandom(void)
{
  //Don't use 0x0000 or 0x8003
  //TODO: Try to generate an unpredictable seed instead of a hard coded value.
  uint16 seed = 0x03A8;  
  RNDL = (uint8)(seed >> 8);
  RNDL = (uint8)seed;
}
/******************************************************************************/
uint16 utils_getRandom(void)
{
  uint16 value;
  
  // read in the current "random" value
  value = RNDH << 8;
  value |= RNDL;
  
  // Clock the shift register once to generate a new value for next time.
  ADCCON1 |= BIT2;
  
  return value;
}